package com.muyu.marketing.bargain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.marketing.bargain.domain.BargainProduct;
import com.muyu.marketing.bargain.domain.req.BargainProductReq;
import com.muyu.marketing.bargain.domain.resp.BargainProductResp;
import com.muyu.marketing.bargain.mapper.BargainProductMapper;
import com.muyu.marketing.bargain.service.BargainProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 砍价商品Service业务层处理
 * 
 * @author muyu
 * @date 2025-01-27
 */
@Service
public class BargainProductServiceImpl extends ServiceImpl<BargainProductMapper, BargainProduct> implements BargainProductService {
    
    @Autowired
    private BargainProductMapper bargainProductMapper;
    
    @Override
    public List<BargainProductResp> selectBargainProductList(BargainProduct bargainProduct) {
        List<BargainProduct> list = bargainProductMapper.selectBargainProductList(bargainProduct);
        return convertToRespList(list);
    }
    
    @Override
    public BargainProductResp selectBargainProductById(Long id) {
        BargainProduct bargainProduct = bargainProductMapper.selectById(id);
        return convertToResp(bargainProduct);
    }
    
    @Override
    public BargainProductResp selectBargainProductByProductId(Long productId) {
        BargainProduct bargainProduct = bargainProductMapper.selectBargainProductByProductId(productId);
        return convertToResp(bargainProduct);
    }
    
    @Override
    public int insertBargainProduct(BargainProductReq bargainProductReq) {
        BargainProduct bargainProduct = new BargainProduct();
        BeanUtils.copyProperties(bargainProductReq, bargainProduct);
        
        // 设置默认值
        bargainProduct.setSales(0);
        bargainProduct.setBargainers(0);
        bargainProduct.setSuccessfulBargains(0);
        bargainProduct.setBargainStatus(0); // 未开始
        bargainProduct.setDeleted(0);
        bargainProduct.setCreateTime(DateUtils.getNowDate());
        
        // 验证砍价规则
        validateBargainRule(bargainProduct);
        
        return bargainProductMapper.insert(bargainProduct);
    }
    
    @Override
    public int updateBargainProduct(BargainProductReq bargainProductReq) {
        BargainProduct bargainProduct = new BargainProduct();
        BeanUtils.copyProperties(bargainProductReq, bargainProduct);
        
        // 验证砍价规则
        validateBargainRule(bargainProduct);
        
        return bargainProductMapper.updateById(bargainProduct);
    }
    
    @Override
    public int deleteBargainProductByIds(Long[] ids) {
        for (Long id : ids) {
            deleteBargainProductById(id);
        }
        return ids.length;
    }
    
    @Override
    public int deleteBargainProductById(Long id) {
        BargainProduct bargainProduct = new BargainProduct();
        bargainProduct.setId(id);
        bargainProduct.setDeleted(1);
        return bargainProductMapper.updateById(bargainProduct);
    }
    
    @Override
    public int enableBargainProduct(Long id) {
        BargainProduct bargainProduct = bargainProductMapper.selectById(id);
        if (bargainProduct == null) {
            throw new RuntimeException("砍价商品不存在");
        }
        
        Date now = new Date();
        if (bargainProduct.getStartTime().after(now)) {
            // 未到开始时间
            return updateBargainProductStatus(id, 0);
        } else if (bargainProduct.getEndTime().before(now)) {
            // 已过结束时间
            return updateBargainProductStatus(id, 2);
        } else {
            // 进行中
            return updateBargainProductStatus(id, 1);
        }
    }
    
    @Override
    public int disableBargainProduct(Long id) {
        return updateBargainProductStatus(id, 2);
    }
    
    @Override
    public int copyBargainProduct(Long id) {
        BargainProduct original = bargainProductMapper.selectById(id);
        if (original == null) {
            throw new RuntimeException("砍价商品不存在");
        }
        
        BargainProduct copy = new BargainProduct();
        BeanUtils.copyProperties(original, copy);
        copy.setId(null);
        copy.setProductName(original.getProductName() + "_复制");
        copy.setSales(0);
        copy.setBargainers(0);
        copy.setSuccessfulBargains(0);
        copy.setBargainStatus(0);
        copy.setCreateTime(DateUtils.getNowDate());
        copy.setUpdateTime(null);
        
        return bargainProductMapper.insert(copy);
    }
    
    @Override
    public int updateBargainProductStatus(Long id, Integer status) {
        BargainProduct bargainProduct = new BargainProduct();
        bargainProduct.setId(id);
        bargainProduct.setBargainStatus(status);
        bargainProduct.setUpdateTime(DateUtils.getNowDate());
        return bargainProductMapper.updateById(bargainProduct);
    }
    
    @Override
    public int updateSales(Long id, Integer sales) {
        return bargainProductMapper.updateSales(id, sales);
    }
    
    @Override
    public int updateStock(Long id, Integer stock) {
        return bargainProductMapper.updateStock(id, stock);
    }
    
    @Override
    public List<BargainProductResp> selectActiveBargainProductList() {
        List<BargainProduct> list = bargainProductMapper.selectActiveBargainProductList();
        return convertToRespList(list);
    }
    
    @Override
    public List<BargainProductResp> selectBargainProductListByStatus(Integer status) {
        List<BargainProduct> list = bargainProductMapper.selectBargainProductListByStatus(status);
        return convertToRespList(list);
    }
    
    /**
     * 验证砍价规则
     */
    private void validateBargainRule(BargainProduct bargainProduct) {
        // 验证砍价价格
        if (bargainProduct.getBargainPrice() == null || bargainProduct.getBargainPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("砍价价格必须大于0");
        }
        
        // 验证最低砍价价格
        if (bargainProduct.getMinBargainPrice() == null || bargainProduct.getMinBargainPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("最低砍价价格必须大于0");
        }
        
        // 验证价格关系
        if (bargainProduct.getOriginalPrice().compareTo(bargainProduct.getBargainPrice()) <= 0) {
            throw new RuntimeException("原价必须大于砍价价格");
        }
        if (bargainProduct.getBargainPrice().compareTo(bargainProduct.getMinBargainPrice()) <= 0) {
            throw new RuntimeException("砍价价格必须大于最低砍价价格");
        }
    }
    
    /**
     * 转换为响应DTO
     */
    private BargainProductResp convertToResp(BargainProduct bargainProduct) {
        if (bargainProduct == null) {
            return null;
        }
        
        BargainProductResp resp = new BargainProductResp();
        BeanUtils.copyProperties(bargainProduct, resp);
        
        // 设置状态描述
        resp.setStatusText(getStatusText(bargainProduct.getBargainStatus()));
        
        return resp;
    }
    
    /**
     * 转换为响应DTO列表
     */
    private List<BargainProductResp> convertToRespList(List<BargainProduct> list) {
        List<BargainProductResp> respList = new ArrayList<>();
        for (BargainProduct bargainProduct : list) {
            respList.add(convertToResp(bargainProduct));
        }
        return respList;
    }
    
    /**
     * 获取状态描述
     */
    private String getStatusText(Integer status) {
        if (status == null) {
            return "未知";
        }
        switch (status) {
            case 0:
                return "未开始";
            case 1:
                return "进行中";
            case 2:
                return "已结束";
            default:
                return "未知";
        }
    }
    
}